//
//  HistoryVC.m
//  MyDemo
//
//  Created by jay on 2020/8/23.
//  Copyright © 2020 jay. All rights reserved.
//

#import "HistoryVC.h"
#import "HistoryVM.h"
#import "HistoryCell.h"
#import "HistoryDetailVC.h"

@interface HistoryVC ()

@property (nonatomic, strong) HistoryVM *curVM;

@end

@implementation HistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"调用记录";
            
    self.myTableView.backgroundColor = TX_COLOR_FROM_RGB(0xFAFAFA);
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.myTableView registerClass:[HistoryCell class] forCellReuseIdentifier:kTableCellID];
        
    TX_WS(weakSelf);
    
    // 集成下拉刷新
    [self setHeaderRereshingBlock:^{
        
        [weakSelf reqGetData:YES isReqMore:NO];

    } kRefreshingHeaderClass];
    
    // 集成上拉加载更多
    [self setFooterRereshingBlock:^{
        
        [weakSelf reqGetData:YES isReqMore:YES];
    }];
    
    //请求数据
    [self reqGetData:NO isReqMore:NO];
}

-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recordRefreshedNotify:) name:kNotifyKeyRecordRefreshed object:nil];
}

-(void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Help Method

//跳转详情
-(void)toHistoryDetailVC:(NSInteger )row {
    
    HistoryDetailVC *vc = [[HistoryDetailVC alloc] init];
    vc.curModel = [self.curVM gitModelForRow:row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Request Method

//请求数据
-(void)reqGetData:(BOOL )isFromRefrehing isReqMore:(BOOL )isReqMore {

    if (!isFromRefrehing) {
        [self showLoading];
    }
    
    TX_WS(weakSelf);
    [self.curVM getDataFromNet:isReqMore completeHandle:^(NSError * _Nonnull error) {
        
        if (isFromRefrehing) {
            if (!isReqMore) {
                [weakSelf headerEndRefreshing];
            } else {
                [weakSelf footerEndRefreshing];
            }
        } else {
            [self hideLoading];
        }
        
        //判断是否隐藏上拉加载更多
        [weakSelf setFooterHidden:!self.curVM.hasNext];

        //先隐藏空状态视图
        [self hideEmptyView];
        
        [weakSelf.myTableView reloadData];
        
        if (self.curVM.dataArr.count <= 0) {
            [self showEmptyImage:@"no_content" title:error?error.localizedDescription:@"暂无记录" buttonName:@"" inView:self.myTableView buttonBlock:^{
            }];
        } else {
            if (error) {
                TX_TOAST(error.localizedDescription);
            }
        }
    }];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.curVM rowNumber];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableCellID];
    
    cell.titleLabel.text = [self.curVM titleForRow:indexPath.row];
    
    cell.titleLabel.textColor = (indexPath.row == 0 ? kTitleColorLatest : kTitleColorNormal);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self toHistoryDetailVC:indexPath.row];
}

#pragma mark - Notification

//记录刷新通知
-(void)recordRefreshedNotify:(NSNotification *)noti
{
    TX_TOAST(@"有新的记录数据，可下拉刷新查看！");
}

#pragma mark - Getter

- (HistoryVM *)curVM {
    if(_curVM == nil) {
        _curVM = [[HistoryVM alloc] init];
    }
    return _curVM;
}

@end
